/**
 * 
 */
package cn.edu.nju.ws.sview.wyge.util;

import java.io.IOException;
import java.nio.CharBuffer;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;

import cn.edu.nju.ws.sview.cache.URIIDCache;
import cn.edu.nju.ws.sview.cache.URILabelCache;
import cn.edu.nju.ws.sview.database.DBConnectionFactory;
import cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetItem;
import cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetName;
import cn.edu.nju.ws.sview.wyge.relations.index.SearchIndexManager;

/**
 * @author Weiyi Ge (geweiyi@gmail.com)
 * 2012-8-27
 *
 */
public class Test {
	public static String testName(String ds_type, int ds_id, String lang){
		String name = DatasetItem.getShowNameByDatasetID(ds_id, ds_type, lang);
		System.out.println("name:"+name);
		return name;
	}
	public void testThread(){
//		T t1 = new Test.T(100, "abc");
//		T t2 = new T(200, "def");
//		Thread th = new Thread(t1);
//		th.start();
//		Thread th2 = new Thread(t2);
//		th2.start();
	}
	public void testSelect(int datasetID){
		boolean result = false;
		try {
			Connection conn = DBConnectionFactory.getConnection();
			PreparedStatement stmt = conn.prepareStatement("select null from custom_dataset where id=?");
			stmt.setInt(1, datasetID) ;
			ResultSet rs = stmt.executeQuery();
			if ( rs.next() ) {
				result = true;
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println(result);
	}
	public void testInsert(){
		String name = Test.testName("uri", 11131139, "en");
		System.out.println("dd:"+SearchIndexManager.getHighlightedText(name, "The OWL 2 Schema vocabulary (OWL 2)"));
//		boolean result = false;
//		try {
//			Connection conn = DBConnectionFactory.getConnection();
//			PreparedStatement stmt = conn.prepareStatement("insert into ds_comment(ds_type,ds_id,time,group_id,lang,text,like_total,dislike_total) values('abcd',7,?,2,'en','tst',1,1)");
//			stmt.setTimestamp(1, new Timestamp(System.currentTimeMillis()));//2012-10-24 21:03:14
////			stmt.setDate(1, new Date(System.currentTimeMillis()));//2012-10-24 00:00:00
//			stmt.execute();
//			stmt.close();
//			conn.close();
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//		System.out.println(result);
	}
	public static void main(String[] argus){
//		new Test().testSelect(51);
//		System.out.println(new Timestamp(System.currentTimeMillis()));
		new Test().testInsert();
//		String a = "";
//		try {
//			a = URIIDCache.getInstance().getURI(10850999);
//					//520465);
////			System.out.println(URILabelCache.getInstance().getLabel(10850999, "en"));
//			DatasetItem ds = new DatasetItem("uri",10850999,a);
//			ArrayList<DatasetName> names = ds.getNames(0, -1);
//			for(DatasetName dn : names){
//				System.out.println(dn.toString());
//			}
//		} catch (Throwable e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		System.out.println("a:"+a);
	}
	public class T implements Runnable{
		int gid;
		String lang;
		public T(int gid, String lang){
			this.gid = gid;
			this.lang = lang;
		}
		
		@Override
		public void run() {
			Connection conn = DBConnectionFactory.getConnection();
			PreparedStatement ipstmt;
			try {
				if(gid==200){
				Thread.sleep(500);
				}
				ipstmt = conn.prepareStatement("INSERT INTO custom_dataset(group_id,create_time,update_time,lang) values(?,now(),now(),?);" );
				ipstmt.setInt(1, gid);
				ipstmt.setString(2, lang);
				ipstmt.executeUpdate();
				System.out.println("gid:"+gid+"|lang:"+lang+"|Sleeping...");
				
				if(gid==100){
				Thread.sleep(1000);
				}
				
				System.out.println("gid:"+gid+"|lang:"+lang+"|Waked...");
				ipstmt = conn.prepareStatement("select last_insert_id();");
				ResultSet rs = ipstmt.executeQuery();
				if(rs.next()){
					System.out.println("gid:"+gid+"|lang:"+lang+"|rs:"+rs.getInt(1));
				}
				rs.close();
				ipstmt.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
}
